'use strict'

const fs = require('fs')
const { Sequelize, DataType } = require('sequelize')
const { db_dialect, db_host, db_username, db_password, db_database } = require('../config/db')

const sequelize = new Sequelize(db_database, db_username, db_password, {
    host: db_host,
    dialect: db_dialect
})

let files = fs.readdirSync(__dirname)
let resFiles = files.filter(item=>{
    return item.endsWith('.js') && item != 'index.js'
})

let resObj = {}
resFiles.forEach(item=>{
    let modelName = item.replace('.js','')
    let tmpObj = require(__dirname+'/'+item)
    let tableName = modelName.toLowerCase()
    resObj[modelName] = sequelize.define(tableName,tmpObj)
})

resObj.sync=async()=>{
    sequelize.sync({force:true}).then(()=>{
        
    })
}

module.exports = resObj